/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.base.web.supplier;

import com.alibaba.fastjson.JSONValidator;
import com.jeesite.common.codec.EncodeUtils;
import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.base.entity.supplier.Supplier;
import com.jeesite.modules.base.entity.supplier.SupplierBusinessInfo;
import com.jeesite.modules.base.service.supplier.SupplierService;
import com.jeesite.modules.custom.enumInfo.supplierTypeEnum;
import com.jeesite.modules.sys.entity.User;
import com.jeesite.modules.sys.service.UserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 供应商Controller
 *
 * @author LV
 * @version 2021-03-01
 */
@Controller
@RequestMapping(value = "${adminPath}/base/supplier/supplier")
public class SupplierController extends BaseController {

    @Autowired
    private SupplierService supplierService;
    @Autowired
    private UserService userService;

    /**
     * 获取数据
     */
    @ModelAttribute
    public Supplier get(String supplierId, boolean isNewRecord) {
        return supplierService.get(supplierId, isNewRecord);
    }

    /**
     * 查询列表
     */
    @RequiresPermissions("base:supplier:supplier:view")
    @RequestMapping(value = {"list", ""})
    public String list(Supplier supplier, Model model) {
        model.addAttribute("listUser", userService.findList(new User()));
        model.addAttribute("supplier", supplier);
        return "modules/base/supplier/supplierList";
    }

    /**
     * 查询列表数据
     */
    @RequiresPermissions("base:supplier:supplier:view")
    @RequestMapping(value = "listData")
    @ResponseBody
    public Page<Supplier> listData(Supplier supplier, HttpServletRequest request, HttpServletResponse response) {
        supplier.setPage(new Page<>(request, response));
        Page<Supplier> page = supplierService.findPage(supplier);
        return page;
    }

    /**
     * 查看编辑表单
     */
    @RequiresPermissions("base:supplier:supplier:view")
    @RequestMapping(value = "form")
    public String form(Supplier supplier, Model model) {
        if (supplier.getIsNewRecord()){
            List<SupplierBusinessInfo> listBusiness = new ArrayList<>();
            SupplierBusinessInfo supplierBusinessInfo1= new SupplierBusinessInfo();
            supplierBusinessInfo1.setBusinessInfoName("征信报告上传");
            supplierBusinessInfo1.setRequired("1");
            listBusiness.add(supplierBusinessInfo1);
            SupplierBusinessInfo supplierBusinessInfo2= new SupplierBusinessInfo();
            supplierBusinessInfo2.setBusinessInfoName("营业执照上传");
            supplierBusinessInfo2.setRequired("1");
            listBusiness.add(supplierBusinessInfo2);
            supplier.setSupplierBusinessInfoList(listBusiness);
        }
        Supplier supplier1 =  new Supplier();
        supplier1.setSupplierType(supplierTypeEnum.BROKER.getCode());
        List<Supplier> supplierList = supplierService.findList(supplier1);
        model.addAttribute("listUser", userService.findList(new User()));
        model.addAttribute("supplierList", supplierList);
        model.addAttribute("supplier", supplier);
        return "modules/base/supplier/supplierForm";
    }

    /**
     * 查看表单详情
     */
    @RequiresPermissions("base:supplier:supplier:view")
    @RequestMapping(value = "detail")
    public String detail(Supplier supplier, Model model) {
        List<Supplier> supplierList = supplierService.findList(new Supplier());
        model.addAttribute("listUser", userService.findList(new User()));
        model.addAttribute("supplierList", supplierList);
        model.addAttribute("supplier", supplier);
        return "modules/base/supplier/supplierDetail";
    }

    /**
     * 保存数据
     */
    @RequiresPermissions("base:supplier:supplier:edit")
    @PostMapping(value = "save")
    @ResponseBody
    public String save(@Validated Supplier supplier) {
        supplierService.save(supplier);
        return renderResult(Global.TRUE, text("保存供应商成功！"));
    }

    /**
     * 删除数据
     */
    @RequiresPermissions("base:supplier:supplier:delete")
    @RequestMapping(value = "delete")
    @ResponseBody
    public String delete(Supplier supplier) {
        supplierService.delete(supplier);
        return renderResult(Global.TRUE, text("删除供应商成功！"));
    }

    /**
     * 列表选择对话框
     */
    @RequiresPermissions("base:supplier:supplier:view")
    @RequestMapping(value = "supplierSelect")
    public String empUserSelect(Supplier supplier, String selectData, Model model) {
        String selectDataJson = EncodeUtils.decodeUrl(selectData);
        if (selectDataJson != null && JSONValidator.from(selectDataJson).validate()){
            model.addAttribute("selectData", selectDataJson);
        }
        model.addAttribute("Supplier", supplier);
        return "modules/base/supplier/supplierSelect";
    }

    @RequiresPermissions("base:supplier:supplier:edit")
    @RequestMapping(value = "checkAbbreviation")
    @ResponseBody
    public String checkAbbreviation( String oldSupplierNameAbbreviation,  String supplierNameAbbreviation) {
        Supplier supplier = new Supplier();
        supplier.setSupplierNameAbbreviation(supplierNameAbbreviation);
        if (supplierNameAbbreviation != null && supplierNameAbbreviation.equals(oldSupplierNameAbbreviation)) {
            return Global.TRUE;
        } else if (supplierNameAbbreviation != null && supplierService.getByAbbreviation(supplier) == null) {
            return Global.TRUE;
        }
        return Global.FALSE;
    }
}